/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.lib.actions;

import com.bff.mpd.musicjukebox.ui.MainFramePlayerTop;
import com.bff.mpd.musicjukebox.ui.SplashScreen;
import com.bff.mpd.utils.Utils;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.objects.MPDArtist;

/**
 *
 * @author bfindeisen
 */
public class PlaylistInsertArtistAction extends AbstractAction {

    private MPDPlaylist playlist;
    private MPDArtist artist;
    private SplashScreen sp;

    public PlaylistInsertArtistAction(MPDPlaylist playlist,
            MPDArtist artist) {
        super(Utils.getMessage("ACTION_INSERT_ARTIST_NAME"));
        this.artist = artist;
        this.playlist = playlist;
        putValue(Action.SHORT_DESCRIPTION, Utils.getMessage("ACTION_INSERT_ARTIST_SHORT_DESC"));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        sp = new SplashScreen(MainFramePlayerTop.getFrame(), false);
        sp.setIndeterminate(true);
        sp.setTitleLabel(Utils.getMessage("PROGRESS_LOADING_ARTIST"));
        sp.setMainTextArea("\n" + java.text.MessageFormat.format(Utils.getMessage("PROGRESS_LOADING_ARTIST_TEXT"), new Object[] {artist.getName()}) + "\n\n");
        sp.setProgressBarLabel(Utils.getMessage("PROGRESS_LOADING_ARTIST_LABEL"));
        sp.pack();

        new Thread(new Runnable() {

            @Override
            public void run() {
                while (!sp.isVisible()) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(PlaylistInsertArtistAction.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                try {
                    playlist.insertArtist(getArtist());
                } catch (Exception ex) {
                    Logger.getLogger(PlaylistInsertArtistAction.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    sp.setStopped(true);
                }
            }
        }).start();
    }

    /**
     * @return the playlist
     */
    public MPDPlaylist getPlaylist() {
        return playlist;
    }

    /**
     * @param playlist the playlist to set
     */
    public void setPlaylist(MPDPlaylist playlist) {
        this.playlist = playlist;
    }

    /**
     * @return the artist
     */
    public MPDArtist getArtist() {
        return artist;
    }

    /**
     * @param artist the artist to set
     */
    public void setArtist(MPDArtist artist) {
        this.artist = artist;
    }
}
